﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Collections;

namespace GDI_AnalysisComponents
{
    //组件类，可序列化

    [Serializable()]
    public class GComponent
    {
        private static int count = 0;
        private int id;
        private string name;
        private string type;
        private float width;
        private float hight;
        private float x;
        private float y;
        private PointF location;
        private GUnit headGUnit;
        private ArrayList paraGUnitList;
        private ArrayList selRects;
        private ArrayList inGunsList;

        public int Count
        {
            get { return count; }
            set { count = value; }
        }

        public int ID
        {
            get { return id; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Type
        {
            get { return type; }
            set { type = value; }
        }

        public float X
        {
            get { return x; }
            set { x = value; }
        }

        public float Y
        {
            get { return y; }
            set { y = value; }
        }

        public PointF Location
        {
            get { return new PointF(this.x, this.y); }
            set { location = value; }
        }

        public float Width
        {
            get { return width; }
            set { width = value; }
        }

        public float Hight
        {
            get { return hight; }
            set { hight = value; }
        }

        public GUnit HeadGUnit
        {
            get { return headGUnit; }
            set { headGUnit = value; }
        }

        public ArrayList ParaGUnitList
        {
            get { return paraGUnitList; }
            set { paraGUnitList = value; }
        }

        public ArrayList SelRects
        {
            get { return this.selRects; }
            set { this.selRects = value; }
        }


        public ArrayList InGunSList
        {
            get { return this.inGunsList; }
            set { this.inGunsList = value; }
        }


        //构造函数
        public GComponent()
        {
        }

        //public GComponent(string name)
        //{
        //    count++;
        //    this.id = count;
        //    this.name = name;           
        //    this.x = 0;
        //    this.y = 0;
        //    this.location = new PointF(this.x, this.y);
        //    this.width = 100;
        //    this.hight = 30;
        //    headGUnit = new GUnit(name, null);
        //    headGUnit.Width = width;
        //    headGUnit.Hight = hight;
        //    headGUnit.X = x;
        //    headGUnit.Y = y;
        //    paraGUnitList = new ArrayList();
        //    selRects = new ArrayList();
        //    inGunsList = new ArrayList();
        //}

        public GComponent(string name, string type)
        {
            count++;
            this.id = count;
            this.name = name;
            this.type = type;
            this.x = 0;
            this.y = 0;
            this.location = new PointF(this.x, this.y);
            this.width = 180;
            this.hight = 30;
            headGUnit = new GUnit(name, null);
            headGUnit.Width = width;
            headGUnit.Hight = hight;
            headGUnit.X = x;
            headGUnit.Y = y;
            paraGUnitList = new ArrayList();
            selRects = new ArrayList();
            inGunsList = new ArrayList();
        }

        //增加参数单元
        public void AddParaGUnit(string name, string type)
        {
            string result=string.Empty;
            if (type.LastIndexOf(".") == -1)
            {
                result = type;
            }
            else
            {
                result = type.Substring(type.LastIndexOf(".")+1);
            }
            GUnit paraGUnit = new GUnit(name, result);
            paraGUnit.GComponentId = id;
            paraGUnitList.Add(paraGUnit);
            paraGUnit.Width = width;
            paraGUnit.Hight = hight / paraGUnitList.Count;
            paraGUnit.X = x;
            paraGUnit.Y = y + hight;
            this.hight = this.hight + 30;
        }

        //绘制组件
        public void DrawGComponent(Graphics g)
        {
            headGUnit.DrawGunit(g);

            object[] arrayOfGUnits = paraGUnitList.ToArray();
            foreach (GUnit gun in arrayOfGUnits)
            {
                gun.DrawGunit(g);
            }

            string text;
            Font font;
            RectangleF rectF = new RectangleF(x, y, width, hight / (paraGUnitList.Count + 1));
            StringFormat strFormat = new StringFormat();

            strFormat.Alignment = StringAlignment.Near;
            strFormat.LineAlignment = StringAlignment.Center;
            strFormat.Trimming = StringTrimming.EllipsisCharacter;

            Pen pen = new Pen(Color.Black);
            SolidBrush textSBrush = new SolidBrush(Color.Black);

            //text = "" + id;
            text = "" + id;
            font = new Font("Verdana", 10, FontStyle.Italic);
            textSBrush = new SolidBrush(Color.Black);

           // g.DrawString(text, font, textSBrush, rectF, strFormat);
            font.Dispose();
        }

        //获取组件整个矩形框
        public RectangleF GetGComponentRect()
        {
            RectangleF rect = new RectangleF(x, y, width, hight);
            return rect;
        }

        //获取组件头矩形
        public RectangleF GetGComponentHeadGUnitRect()
        {
            return headGUnit.getGUnitRect();
        }

        //组件移动
        public void MoveToPointF(PointF pt)
        {
            //this.location = pt;
            this.x = pt.X;
            this.y = pt.Y;
            this.headGUnit.X = pt.X;
            this.headGUnit.Y = pt.Y;
            //this.headGUnit.Location = pt;

            for (int i = 0; i < paraGUnitList.Count; i++)
            {
                GUnit gun = (GUnit)paraGUnitList[i];
                gun.X = pt.X;
                gun.Y = pt.Y + (i + 1) * hight / (paraGUnitList.Count + 1);
            }
        }

        public GComponent Clone(PointF mousePt)
        {
            GComponent gcClone = new GComponent(this.name, this.type);

            gcClone.X = mousePt.X;
            gcClone.Y = mousePt.Y;
            gcClone.Location = mousePt;
            gcClone.headGUnit.X = mousePt.X;
            gcClone.headGUnit.Y = mousePt.Y;
            for (int i = 0; i < this.paraGUnitList.Count; i++)
            {
                GUnit paraGUnit = (GUnit)(this.paraGUnitList[i]);

                gcClone.AddParaGUnit(paraGUnit.Name, paraGUnit.Type);
            }

            return gcClone;
        }

        //public void TransformShape(PointF preMousePt, PointF curMousePt, TransformShapeMode tsm)
        //{
        //    float difx = curMousePt.X - preMousePt.X;
        //    float dify = curMousePt.Y - preMousePt.Y;

        //    switch (tsm)
        //    {
        //        case TransformShapeMode.TransformShape_LU:
        //            this.width -= difx;
        //            this.hight -= dify;
        //            this.x = curMousePt.X-difx;
        //            this.y = curMousePt.Y-dify;
        //            break;
        //        case TransformShapeMode.TransformShape_RU:
        //            this.width += difx;
        //            this.hight -= dify;
        //            this.y = curMousePt.Y + dify;  
        //            break;
        //        case TransformShapeMode.TransformShape_RD:
        //            this.width += difx;
        //            this.hight += dify;               
        //            break;
        //        case TransformShapeMode.TransformShape_LD:
        //            this.width -= difx;
        //            this.hight += dify;
        //            this.x = curMousePt.X - difx;
        //            break;
        //    }

        //    this.location = new PointF(this.X,this.Y);
        //    this.headGUnit.X = this.X;
        //    this.headGUnit.Y = this.Y;
        //    this.headGUnit.Location = this.location;
        //    this.headGUnit.Width = width;
        //    this.headGUnit.Hight = hight / (paraGUnitList.Count+1);

        //    for (int i = 0; i < paraGUnitList.Count; i++)
        //    {
        //        GUnit gun = (GUnit)paraGUnitList[i];
        //        gun.X = location.X;
        //        gun.Y = location.Y + (i + 1) * hight / (paraGUnitList.Count + 1);
        //        gun.Width = width;
        //        gun.Hight = hight /( paraGUnitList.Count+1);
        //    }
        //}

    }
}
